<?php
// Logo扫光
if (_van('is_super_logo')) {
    function super_logo(){
        wp_enqueue_style('super_logo', trailingslashit(get_stylesheet_directory_uri()) . 'assets/css/beautify/logo.css');
    }
    add_action( 'wp_enqueue_scripts', 'super_logo' );
}

//禁止修订版本
if(_van('is_optimization')){
    add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
    function specs_wp_revisions_to_keep( $num, $post ) {
    return 0;
}
}

//复制弹窗版权信息
if (_van('is_copy_window')) {
    function zm_copyright_tips() {
        $copy_window_text=_van('copy_window_text');
        echo '<link rel="stylesheet" type="text/css" rel="external nofollow" target="_blank" href="https://cdn.bootcdn.net/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" >';
        echo '<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>';
        echo '<script>document.body.oncopy = function() { swal("复制成功！", "' . _van('copy_window_text', '$copy_window_text') . '","success");};</script>';
    }
    add_action('wp_footer', 'zm_copyright_tips', 100);
}
//去除后台多语言功能
if(_van('is_delete_admin_login_language',"0") === "1"){
    add_filter( 'login_display_language_dropdown', '__return_false' );  
}

//+----------------------------------------------------------------------
//| 自定义标签后缀变量 [ 打开标签筛选页标题会按这个同步,更利于SEO优化 ] 
//+----------------------------------------------------------------------
function tag_title(){
        //申明title是一个数组
          $title = array(
            'title' => '',
        );
        //判断是否是标签
        if (is_tag()) {
            //获取后台的配置内容
            $tag_variable = _van('tag_variable');
            //把后台获取的内容放进数组
            $title['title'] = single_term_title('', false).''.$tag_variable;
            //获取站点名字
            $title['site'] = get_bloginfo('name', 'display');
            //获取_；
            $sep = apply_filters('document_title_separator', '-');
            //讲_分割放进数组
            $title = implode("$sep", array_filter($title));
            //返回数组
            return $title;
        }
    }
if(_van('is_tag_variable', "0") === "1"){
 add_filter( 'pre_get_document_title', 'tag_title',99);
}

//标题更换
if(_van('is_leave_site_title')){
    function leave_site_title(){
    $title=_van('leave_title');
 echo <<<EOT
    <script>
  document.addEventListener('visibilitychange',function(){if(document.visibilityState=='hidden'){normal_title=document.title;document.title='$title';}else{document.title=normal_title;}});
    </script>
EOT;
}
    add_action('wp_footer','leave_site_title');
}

/* 删除文章时删除图片附件 */  
if(_van('is_delete_article') ){
function delete_post_and_attachments($post_ID) {  
        global $wpdb;  
        //删除特色图片  
        $thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );  
        foreach ( $thumbnails as $thumbnail ) {  
        wp_delete_attachment( $thumbnail->meta_value, true );  
        }  
        //删除图片附件  
        $attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );  
        foreach ( $attachments as $attachment ) {  
        wp_delete_attachment( $attachment->ID, true );  
        }  
        $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );  
} 
    add_action('before_delete_post', 'delete_post_and_attachments');  
}

//点击随机增加阅读量
if(_van('is_read_views')){

    function read_views() {
        $read_views=_van('read_views');
        header('Content-type:application/json; Charset=utf-8');
        $post_id = !empty($_POST['post_id']) ? (int) $_POST['post_id'] : 0;
        $this_num = (int)(rand(0,$read_views) + (int)get_post_meta($post_id,'views',true));
        $new_num = $this_num+1;
        if ($new_num < 0 ) {
        $new_num = 1;
        }
        if ($post_id && update_post_meta( $post_id, 'views', $new_num )) {
        echo json_encode(array('status' => '1', 'msg' => 'PostID：' . $post_id . ' views +1'));exit;
        } else {
        echo json_encode(array('status' => '0', 'msg' => 'post views error'));exit;
        }
}
    add_action('wp_ajax_zb_add_post_views', 'read_views',1);
    add_action('wp_ajax_nopriv_zb_add_post_views', 'read_views',1);
}

//+----------------------------------------------------------------------
//| 外链跳转 [ 访问其他网站会有提示,设置白名单之后则无提示 ] 开始
//+----------------------------------------------------------------------

if(_van('is_go_link')){
    function go_link($content)
    {
        preg_match_all('|<a.*?href="(.*?)".*?>(.*?)\</a>|', $content, $domain_arr, PREG_SET_ORDER);
        $url_whiteList_arr = preg_split('/[;\r\n]+/s', _van('white_list'));
        foreach ($domain_arr as $item) {
            $isreplace = true;
            foreach ($url_whiteList_arr as $url_item) {
                $re = stripos($item[1], $url_item);
                if ($re != false) {
                    $isreplace = false;
                    break;
                }
            }
            if ($isreplace == true) {
                $html = str_replace_once($item[1], get_stylesheet_directory_uri().'/template-parts/diy/go-link.php?a=' . base64_encode($item[1]), $item[0]);
                $content = str_replace($item[0], $html, $content);
            }
        }
        return $content;
}
function str_replace_once($needle, $replace, $haystack)
    {//只替换一次字符串
        $pos = strpos($haystack, $needle);
        if ($pos === false) {
            return $haystack;
        }
        return substr_replace($haystack, $replace, $pos, strlen($needle));
    }
add_filter('the_content', 'go_link');
}

//鼠标指针美化
if (_van('is_diy_mouse')){

add_action('wp_footer', function () {
    
 $body_mouse_img=_van('body_mouse_img');
 $hover_mouse_img=_van('hover_mouse_img');
        echo <<<EOT
<style>body {
cursor: url($body_mouse_img), default;
}
a:hover{cursor:url($hover_mouse_img), pointer;}
</style>
EOT;
    }, 100);
}

//鼠标跟随特效美化
if(_van('is_mouse_follow')){
    function zm_mouse_cursor() { ?>
<!-- 必须的DIV -->
<div class="mouse-cursor cursor-outer"></div>
<div class="mouse-cursor cursor-inner"></div>
<!-- JS脚本 -->
<script>
jQuery(document).ready(function($){
	var myCursor = jQuery('.mouse-cursor');
	if (myCursor.length) {
		if ($('body')) {
			const e = document.querySelector('.cursor-inner'),
			t = document.querySelector('.cursor-outer');
			let n,
			i = 0,
			o = !1;
			window.onmousemove = function(s) {
				o || (t.style.transform = "translate(" + s.clientX + "px, " + s.clientY + "px)"),
				e.style.transform = "translate(" + s.clientX + "px, " + s.clientY + "px)",
				n = s.clientY,
				i = s.clientX
			},
			$('body').on("mouseenter", "a, .cursor-pointer",
			function() {
				e.classList.add('cursor-hover'),
				t.classList.add('cursor-hover')
			}),
			$('body').on("mouseleave", "a, .cursor-pointer",
			function() {
				$(this).is("a") && $(this).closest(".cursor-pointer").length || (e.classList.remove('cursor-hover'), t.classList.remove('cursor-hover'))
			}),
			e.style.visibility = "visible",
			t.style.visibility = "visible"
		}
	}
})
 
</script>
<!-- 样式 -->
<style>
.mouse-cursor {
	position: fixed;
	left: 0;
	top: 0;
	pointer-events: none;
	border-radius: 50%;
	-webkit-transform: translateZ(0);
	transform: translateZ(0);
	visibility: hidden
}
 
.cursor-inner {
	margin-left: -3px;
	margin-top: -3px;
	width: 6px;
	height: 6px;
	z-index: 10000001;
	background: #ffa9a4;
	-webkit-transition: width .3s ease-in-out,height .3s ease-in-out,margin .3s ease-in-out,opacity .3s ease-in-out;
	transition: width .3s ease-in-out,height .3s ease-in-out,margin .3s ease-in-out,opacity .3s ease-in-out
}
 
.cursor-inner.cursor-hover {
	margin-left: -18px;
	margin-top: -18px;
	width: 36px;
	height: 36px;
	background: #ffa9a4;
	opacity: .3
}
 
.cursor-outer {
	margin-left: -15px;
	margin-top: -15px;
	width: 30px;
	height: 30px;
	border: 2px solid #ffa9a4;
	-webkit-box-sizing: border-box;
	box-sizing: border-box;
	z-index: 10000000;
	opacity: .5;
	-webkit-transition: all .08s ease-out;
	transition: all .08s ease-out
}
 
.cursor-outer.cursor-hover {
	opacity: 0
}
 
.main-wrapper[data-magic-cursor=hide] .mouse-cursor {
	display: none;
	opacity: 0;
	visibility: hidden;
	position: absolute;
	z-index: -1111
}
</style>
<?php }
add_action( 'wp_footer', 'zm_mouse_cursor' );
}

//鼠标点击爆炸特效
if (_van('is_click_boom')) {
    add_action('wp_footer', function () {
    $theme_url = get_stylesheet_directory_uri();
        echo <<<EOT
<canvas class="fireworks" style="position:fixed;left:0;top:0;z-index:99999999;pointer-events:none;"></canvas>
<script type="text/javascript" src="$theme_url/assets/js/beautify/click_boom.js"></script>
EOT;
    }, 100);
}

//鼠标点击爱心特效
if(_van('is_click_love')){
    add_action('wp_footer', function () {
        $theme_url = get_stylesheet_directory_uri();
        echo <<<EOT
        <canvas class="fireworks" style="position:fixed;left:0;top:0;z-index:99999999;pointer-events:none;"></canvas>
        <script type="text/javascript" src="$theme_url/assets/js/beautify/click_love.js"></script>
EOT;
    }, 100);
    
}

//+----------------------------------------------------------------------
//| 网站防复制 [ 开启对应得功能网站执行对应得禁止 ] 开始
//+----------------------------------------------------------------------
$exclude_switch = true;
        if (_van('enable_adminer') && current_user_can('administrator')){
            $exclude_switch = false;
        }
        if (_van('enable_loginer') && is_user_logged_in()) {
            $exclude_switch = false;
        }

        if($exclude_switch){
            if (_van('disable_right_click')) {
                add_action('wp_footer', function () {
        echo <<<EOT
<script language="Javascript">
                    document.oncontextmenu=new Function("event.returnValue=false");
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_select_text')) {
                add_action('wp_footer', function () {
        echo <<<EOT
                <style type="text/css">
                    body{
                        -moz-user-select:none; /*火狐*/
                        -webkit-user-select:none; /*webkit浏览器*/
                        -ms-user-select:none; /*IE10*/
                        -khtml-user-select:none; /*早期浏览器*/
                        user-select:none;
                    }
                </style>
                <script language="Javascript">
             document.ondragstart=function(){return false};
		    document.onselectstart =function(){return false};
		    document.onbeforecopy=function(){return false};
		    document.onmouseup=function(){document.selection.empty();};
			document.onselect=function(){document.selection.empty();};
			document.oncopy=function(){document.selection.empty();};
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_drag_image')) {
                         add_action('wp_footer', function () {
        echo <<<EOT
      <script type='text/javascript'>
                   function imgdragstart(){return false;}
                   for(i in document.images)document.images[i].ondragstart=imgdragstart;
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_f12')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
                <script type='text/javascript'>
                 

    				//禁用开发者工具F12
    				document.onkeydown = function () {
       				 if (window.event && window.event.keyCode == 123) {
            			event.keyCode = 0;
           			 event.returnValue = false;
            		return false;
        			}
   					 };
    				//如果用户在工具栏调起开发者工具，那么判断浏览器的可视高度和可视宽度是否有改变，如有改变则关闭本页面 
    				window.onresize = function () {
    				    var w = document.documentElement.scrollWidth || document.body.scrollWidth;
                        var h = document.documentElement.scrollHeight || document.body.scrollHeight;
        			if (h != window.innerHeight || w != window.innerWidth) {
           			 window.close();
            		window.location = "about:blank";
        			}
    				}
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_print')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
                <style>
                    @media print{
                        body{display:none}
                    }
                </style>

                <script type="text/javascript">

                    document.onkeydown = function () {
                        if ((window.event && window.event.keyCode == 80) || ((event.ctrlKey))) {
                            event.keyCode = 0;
                            event.returnValue = false;
                            return false;
                        }
                    };
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_view_source')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
                <style>
                    @media print{
                        body{display:none}
                    }
                </style>

                <script type="text/javascript">

                    document.onkeydown = function () {
                        if ((window.event && window.event.keyCode == 80) || ((event.ctrlKey))) {
                            event.keyCode = 0;
                            event.returnValue = false;
                            return false;
                        }
                    };
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_save_page')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
      <script>
                    window.onkeydown = function(e) {
                        if (e.ctrlKey && e.keyCode == 83) {
                            e.preventDefault();
                            e.returnValue = false;
                            return false;
                        }
                    }
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_select_all')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
        <script type="text/javascript">

                    document.onkeydown = function () {
                        if ((window.event && window.event.keyCode == 65) || ((event.ctrlKey))) {
                            event.keyCode = 0;
                            event.returnValue = false;
                            return false;
                        }
                    };
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_copy_content')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
              <script type="text/javascript">
                    document.onkeydown = function () {
                        if ((window.event && window.event.keyCode == 123) || ((event.ctrlKey) || (window.event.keycode == 67))) {
                            event.keyCode = 0;
                            event.returnValue = false;
                            return false;
                        }
                    };
                </script>
EOT;
    }, 100);
            }
            if (_van('disable_cut_content')) {
                             add_action('wp_footer', function () {
        echo <<<EOT
    <script type="text/javascript">
                    document.onkeydown = function () {
                        if ((window.event && window.event.keyCode == 88) || ((event.ctrlKey) || (window.event.keycode == 123))) {
                            event.keyCode = 0;
                            event.returnValue = false;
                            return false;
                        }
                    };
                </script>
EOT;
    }, 100);
            }
        }

//+----------------------------------------------------------------------
//| 网站防复制 [ 开启对应得功能网站执行对应得禁止 ] 结束
//+----------------------------------------------------------------------

//+----------------------------------------------------------------------
//| 获取交易弹幕 [ 右下角交易弹幕,可自定义背景颜色与数量 ] 开始
//+----------------------------------------------------------------------

/**
 * 获取订单类型文本
 * @Author   Dadong2g
 * @DateTime 2021-04-23T13:28:14+0800
 * @param    [type]                   $order [description]
 * @return   [type]                          [description]
 */
function get_order_type_text($order) {

    if ($order->order_type == 'charge') {
        $name = site_mycoin('name') . esc_html__('充值', 'ripro-v5');
    } elseif ($order->order_type == 'other') {
        $order_info = maybe_unserialize($order->order_info);
        # 购买会员...
        if (!empty($order_info['vip_day']) && $order_info['vip_day'] > 0) {
            $day  = ($order_info['vip_day'] == 9999) ? esc_html__('永久', 'ripro-v5') : $order_info['vip_day'] . esc_html__('天', 'ripro-v5');
            $name = esc_html__('开通会员', 'ripro-v5') . '-' . $day;
        } elseif (get_post_type($order->post_id) == 'page' && $order->user_id > 0) {
            $name = esc_html__('开通会员', 'ripro-v5');
        } elseif (!get_permalink($order->post_id)) {
            $name = esc_html__('其他订单', 'ripro-v5') . $order->post_id;
        } else {
            $name = get_the_title($order->post_id);
        }
    } else {
        $name = esc_html__('其他订单', 'ripro-v2');
    }

    return $name;

}

if(_van('barrage')){
    function getbarrage($data){
    global $wpdb;
        $sql = "SELECT * FROM wp_cao_order WHERE status='1' and order_type='other' ORDER BY id DESC LIMIT 20";
        $order = $wpdb->get_results($sql, ARRAY_A);
		$data = array();
		foreach ($order as $key => $value) {
				$avatar = get_avatar($value['user_id']);
				$user = new WP_User($value['user_id']);
			    $now_user_link = $_SERVER['HTTP_HOST'].'/author/'.$user->data->display_name;
			
			    if(strstr(get_order_type_text((object)$value),"开通")){
			         $content = "<span>￥".$value['order_price']."</span>&nbsp;<a target=_blank href='".get_permalink($value['post_id'])."' style='color: #fff;'>".get_order_type_text((object)$value)."</a>\n";
			    }else {
			        $content = "<span>￥".$value['order_price']."</span>&nbsp;购买了&nbsp;<a target=_blank href='".get_permalink($value['post_id'])."' style='color: #fff;'>".get_order_type_text((object)$value)."</a>\n";
			    }
			   
				$data[]= 
					array(
						'type'   => 'barrage',
						'now_user_link'    => $now_user_link,
						'avatar'   => $avatar,
						'content'  => $content,
						'barrage_diy_num' => _van('barrage_diy_num'),
		                'barrage_diy_bgcolor' => _van('barrage_diy_bgcolor'),
					);
			    
		} //End foreach
      
		//输出模式
        echo json_encode($data);
        exit;
	}

//wp_ajax_nopriv_ 是在没有登录的处理流程;wp_ajax_是登录后的处理流程
add_action( 'wp_ajax_nopriv_getbarrage', 'getbarrage' );
add_action( 'wp_ajax_getbarrage', 'getbarrage' );

//引用JS和CSS
function get_barrage_addInfo(){
    wp_enqueue_style('barrage', trailingslashit(get_stylesheet_directory_uri()) . 'assets/css/footer/footer.css');
    wp_enqueue_script('barrage', trailingslashit(get_stylesheet_directory_uri()) . 'assets/js/barrage.js');
}
add_action( 'wp_footer', 'get_barrage_addInfo');
}

//+----------------------------------------------------------------------
//| 获取交易弹幕 [ 右下角交易弹幕,可自定义背景颜色与数量 ] 结束
//+----------------------------------------------------------------------

//滚动条美化
if(_van('is_scrollbar')){
     add_action('wp_footer', function () {
        $theme_url = get_stylesheet_directory_uri();
        echo <<<EOT
        <link rel='stylesheet'  href="$theme_url/assets/css/beautify/scrollbar.css"/>
EOT;
    }, 100);
}

//文字输入爆炸效果
if(_van('is_text_boom')){
add_action('wp_footer', function () {
if (is_single() || is_page() ) {
    $theme_url = get_stylesheet_directory_uri();
     echo <<<EOT
    <script type="text/javascript" src="$theme_url/assets/js/beautify/text_boom.js"></script>
    <script>
    POWERMODE.colorful = true; // true 为启用礼花殊效
    POWERMODE.shake = true; // false 为禁用震惊殊效
    document.body.addEventListener('input', POWERMODE);
    </script>
EOT;
}
}, 100);
}

//樱花特效
if (_van('is_cherry')) {
    add_action('wp_footer', function () {
        $theme_url = get_stylesheet_directory_uri();
        echo <<<EOT
    <script type="text/javascript" src="$theme_url/assets/js/beautify/snowfall.js"></script>
    <div class="meiha"></div>
    <style>
        .meiha {
            position: fixed;
            top: 0;
            right: 0;
            z-index: 999;
            width: 350px;
            height: 231px;
            pointer-events: none;
        }

        .snowfall-flakes {
            pointer-events: none;
            animation: sakura 1s linear 0s infinite;
        }

        .snowfall-flakes {
            animation: sakura 1s linear 0s infinite;
        }

        .night .snowfall-flakes {
            background: transparent !important;
        }

        @keyframes sakura {
            0% {
                transform: rotate3d(0, 0, 0, 0deg);
            }

            25% {
                transform: rotate3d(1, 1, 0, 60deg);
            }

            50% {
                transform: rotate3d(1, 1, 0, 0deg);
            }

            75% {
                transform: rotate3d(1, 0, 0, 60deg);
            }

            100% {
                transform: rotate3d(1, 0, 0, 0deg);
            }
        }
    </style>
EOT;
    }, 100);
}